<?php

namespace App\Services\Manage;

use Illuminate\Support\Facades\DB;
use App\Services\User\App;

class Api
{

    // 获取统计数据
    public function getCountData ($table = '')
    {
        $table = $table ? 'api' . '_' . $table : 'api';
        $count = DB::table($table)->select(DB::raw('count(*) AS sum, job_schedule'))
            ->where('status', 1)
            ->groupBy('job_schedule')
            ->orderBy('job_schedule')
            ->get();
        $countRewrite = DB::table($table)->select(DB::raw('count(*) AS rewrite_sum, job_schedule'))
            ->where('status', 1)
            ->where('is_rewrite', 1)
            ->groupBy('job_schedule')
            ->orderBy('job_schedule')
            ->get();
        foreach ($count as $k => & $item) {
            if (is_object($item))
                $item = (array) $item;

            if (count($countRewrite) === 0){
                $item['is_rewrite'] = 0;
            } else {
                $item['is_rewrite'] = is_array($countRewrite[$k]) ? $countRewrite[$k]['rewrite_sum'] : ($countRewrite[$k])->rewrite_sum;
            }

            $count[$k] = $item;
        }
        return $count;
    }

    // 根据传入的api_cate_id,获取相应的接口数据
    public function getCateApis ($data, $table = '')
    {
        ValidateApi::checkValidate($data, ['api_cate_id']);

        $catetable = $table ? 'api_cate' . '_' . $table : 'api_cate';
        $table = $table ? 'api' . '_' . $table : 'api';

        $list = DB::table($table)->leftJoin($catetable, $table . '.api_cate_id', '=', $catetable . '.id')
            ->select($table . '.*', $catetable . '.ver')
            ->where($table . '.status', '=', 1)
            ->where($table . '.api_cate_id', '=', $data['api_cate_id'])
            ->orderBy($table . '.job_schedule', 'desc')
            ->orderBy($table . '.order', 'ASC')->get();
//        $list = Db::view('api', '*')
//            ->view('api_cate', 'ver', 'api.api_cate_id=api_cate.id')
//            ->where(['api.status' => 1, 'api.api_cate_id' => $data['api_cate_id']])
//            ->order('api.job_schedule DESC, api.order ASC')
//            ->select();
        return $list;
    }
//    // 增加一个接口
//    public function addApi ($data)
//    {
//        return $this->model('app\manage\model\Api')->save($data);
//    }
//    // 删除一个接口
//    public function removeApi ($data)
//    {
//        $api['id'] = $data['id'];
//        $api['status'] = 0;
//        return $this->model('app\manage\model\Api')->update($api);
//    }

    /**
     *
     * @param array $data
     * @return mixed $res
     * @throws \App\Exceptions\RequestFailException
     */
    public function rewriteapi($data = [])
    {
        ValidateApi::checkValidate($data, ['id', 'rewrite_status'], true, ['requiredId' => true]);

        $res = \App\Models\Manage\Api::where('id', $data['id'])->update([
            'is_rewrite'    => $data['rewrite_status']
        ]);
        return $res;
    }

    // 获取总后台的app_id
    public static function getAppId() {
        return config('tpproject.yayi_admin_app_id');
    }


    /**
     * @author:      李颖
     * @DateTime:    2017-07-21 16:09:21
     * @Description: 使用场景： 若app_id为总后台，则在进行操作分类时，不加app_id的where条件
     */
    public function _addAppId($app_id)
    {
        $where = [];
        $manageAppId = $this->getAppId();
        if($manageAppId != $app_id && !empty($app_id)){
            $where[] = ['app_id','=',$app_id];
        }
        //$manageAppId != $app_id  && $where[] = ['app_id','=',$app_id];
        return $where;
    }
    /*判断是不是总后台app_id*/
    public function _isManageAppId($app_id): bool
    {
        $manageAppId = $this->getAppId();
        if ($manageAppId == $app_id) {
            return true;
        } else {
            return false;
        }
    }
    // 获取一个app_key app_key以后可以从redis中读取
    public static function getAppKey() {
        return config('tpproject.yayi_admin_app_key');
    }

    //添加app_id
    public static function addAppId($data){
        $app_key = self::getAppkey();
        $find = (new App())->find(['app_key'=>$app_key]);
        $data['app_id'] = $find['res']['id'];
        return $data;
    }
}